package com.woniuxy.mesboot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.mesboot.dto.InspectionRecordDTO;
import com.woniuxy.mesboot.service.InspectionRecordService;
import com.woniuxy.mesboot.util.Result;
import com.woniuxy.mesboot.vo.InspectionRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ：lisir
 * @date ：Created in 2023/5/25 16:55
 * @description：TODO
 */
@RestController
@RequestMapping("/irs")
@Slf4j
public class InspectionRecordController {

    @Resource
    private InspectionRecordService inspectionRecordService;

    @PostMapping
    public Result add(@RequestBody InspectionRecordDTO inspectionRecordDTO) {
        int i = inspectionRecordService.insertSelective(inspectionRecordDTO);
        if (i > 0) {
            return Result.ok("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        int i = inspectionRecordService.deleteByPrimaryKey(id);
        if (i > 0) {
            return Result.ok("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody InspectionRecordDTO inspectionRecordDTO) {
        int i = inspectionRecordService.updateByPrimaryKeySelective(inspectionRecordDTO);
        if (i > 0) {
            return Result.ok("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @GetMapping("{id}")
    public Result queryById(@PathVariable Long id) {
        InspectionRecordVO inspectionRecordVO = inspectionRecordService.selectByPrimaryKey(id);
        if (inspectionRecordVO != null) {
            return Result.ok().data("inspectionRecord",inspectionRecordVO);
        } else {
            return Result.error("查询失败");
        }
    }

    @PostMapping("queryAll")
    public Result queryAll(@RequestBody InspectionRecordDTO inspectionRecordDTO) {
        PageHelper.startPage(inspectionRecordDTO.getCurrentPage(), inspectionRecordDTO.getPageSize(), true);
        List<InspectionRecordVO> list = inspectionRecordService.selectAll(inspectionRecordDTO);
        PageInfo<InspectionRecordVO> pageInfo = new PageInfo<>(list);
        return Result.ok().data("list",list).data("total", pageInfo.getTotal());
    }
}
